#!/bin/bash

PROG=$(basename ${0})
PORTDIR_OVERLAY="/usr/local/portage"
DB="/var/db/pkg"
filesindex=0

declare -a exclude
declare -a files

function usage {
	echo "Usage: $PROG [options] <files>"
	echo
	echo "Options:"
	echo " -h|--help                 Show this message"
	echo " -c|--category <category>  Specifies the category for the package to be created"
	echo " -p|--pkgname <name>       Specifies the name for the package to be created"
	echo " -v|--version <version>    Specifies the version for the package to be created"
	echo " --provide <provide>       Specifies the contents of the PROVIDES line in the ebuild"
	echo "                           packaged. This option can be specified multiple times"
	echo
	echo "Parameters:"
	echo " files                     These are the existing files that are to be packaged"
}

function create_ebuild {
	VARDBDIR="${DB}/${category}/${pkgname}-${pkgver}"
	EBUILD="${VARDBDIR}/${pkgname}-${pkgver}.ebuild"

	mkdir -p $VARDBDIR
	cat > $EBUILD <<EOE
# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# \$Header: /var/cvsroot/gentoo/src/installer/src/misc/mkvardb,v 1.5 2005/08/23 13:47:34 agaffney Exp $

inherit eutils

DESCRIPTION="This is a sample skeleton ebuild file"
HOMEPAGE=""
SRC_URI=""
LICENSE=""
SLOT="0"
KEYWORDS="$(portageq envvar ARCH)"
IUSE=""
DEPEND=""
PROVIDE="${provide}"
EOE
}

function echo_parent_dirs {
	dir=$1
	while $(/bin/true); do
		tmpdir=$(dirname ${dir})
		[ "$tmpdir" = "/" ] && break
		echo $tmpdir
		dir=$tmpdir
	done
}

function sort_dirs_and_files {
	curdir=""

	rm /tmp/mkvardb_filelist 2>/dev/null
	for i in $(ls -1AR --color=no ${files[@]} | sed -e 's/:$//' | grep -ve '^$'$); do
		if [ -d ${i} ]; then
			if [ "${curdir}" != "$(dirname ${i})" ]; then
				echo_parent_dirs ${i} >> /tmp/mkvardb_filelist
			fi
			echo ${i} >> /tmp/mkvardb_filelist
			curdir=${i}
		else
			[ -d "${curdir}/${i}" ] && continue
			echo "${curdir}/${i}" >> /tmp/mkvardb_filelist
		fi
	done
	sort -u /tmp/mkvardb_filelist | sed -e 's://:/:'
}

function create_vardb {
	VARDBDIR="${DB}/${category}/${pkgname}-${pkgver}"
#	mkdir -p $VARDBDIR
	cd $VARDBDIR
#	cp $EBUILD $VARDBDIR
	touch ASFLAGS CATEGORY CBUILD CC CDEPEND CFLAGS CHOST CONTENTS COUNTER CTARGET CXX CXXFLAGS DEPEND EXTRA_ECONF EXTRA_EINSTALL EXTRA_EMAKE FEATURES INHERITED IUSE LDFLAGS LDFLAGS LIBCFLAGS LIBCXXFLAGS LICENSE PDEPEND PF PKGUSE PROVIDE RDEPEND RESTRICT SLOT USE
	echo ${category} > CATEGORY
	echo $(portageq envvar CFLAGS) > CFLAGS
	echo $(portageq envvar CHOST) > CHOST
	echo $(portageq envvar CTARGET) > CTARGET
	echo $(portageq envvar CXXFLAGS) > CXXFLAGS
	echo eutils > INHERITED
	echo ${pkgname}-${pkgver} > PF
	echo 0 > SLOT
	echo $(portageq envvar USE) > USE
    echo ${provide} > PROVIDE
	for i in $(sort_dirs_and_files); do
		if [ -d ${i} ]; then
			echo "dir ${i}" >> CONTENTS
		else
			time=$(stat -c %Y ${i})
			md5=$(md5sum ${i} | cut -d ' ' -f 1)
			echo "obj ${i} $md5 $time" >> CONTENTS
		fi
	done
}

# Parse args
params=${#}
while [ ${#} -gt 0 ]
do
	a=${1}
	shift
	case "${a}" in

	-h|--help)
		usage
		exit 0
		;;

	-c|--category)
		category=$1
		shift
		;;

	-p|--pkgname)
		pkgname=$1
		shift
		;;

	-v|--pkgversion)
		pkgver=$1
		shift
		;;

	--provide)
		provide=$1
		shift
		;;

	-*)
		echo "You have specified an invalid option: ${a}" 1>&2
		usage
		exit 1
		;;

	*)
		files[$filesindex]=$a
		filesindex=$(expr $filesindex + 1)
		;;
		
	esac
done

if [ "$category" = "" ]; then
	echo "You must specify a category" 1>&2
	usage
	exit 1
fi
if [ "$pkgname" = "" ]; then
	echo "You must specify a package name" 1>&2
	usage
	exit 1
fi
if [ "$pkgver" = "" ]; then
	echo "You must specify a package version" 1>&2
	usage
	exit 1
fi
if [ $filesindex -eq 0 ]; then
	echo "You must specify files to include in the package" 1>&2
	usage
	exit 1
fi

create_ebuild
create_vardb
